#nullable enable

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus;
using Volo.Abp.EventBus.Local;

namespace PHMEE.Managers.Diagnosis;

/// <summary>
/// 振动报警-故障诊断事件处理器
/// </summary>
public class VibAlarmEventHandler : ILocalEventHandler<VibAlarmEvent>, ITransientDependency
{
    private readonly IFaultDiagnosisDomainService _faultDiagnosisDomainService;
    private readonly ILogger<VibAlarmEventHandler> _logger;

    public VibAlarmEventHandler(IFaultDiagnosisDomainService faultDiagnosisDomainService, ILogger<VibAlarmEventHandler> logger)
    {
        _faultDiagnosisDomainService = faultDiagnosisDomainService;
        _logger = logger;
    }
    
    public async Task HandleEventAsync(VibAlarmEvent eventData)
    {
        _logger.LogInformation("处理振动报警事件 VibAlarmEvent, PointId: {pointId}, Timestamp: {timestamp}", eventData.PointId, eventData.Timestamp);
        await _faultDiagnosisDomainService.FaultDiagnosisDispatcherAsync(eventData.PointId, eventData.Timestamp);
    }
}

/// <summary>
/// 振动报警-故障诊断时间发布器
/// </summary>
public class VibAlarmEventPublisher : ITransientDependency
{
    private readonly ILocalEventBus _localEventBus;
    private readonly ILogger<VibAlarmEventPublisher> _logger;

    public VibAlarmEventPublisher(ILocalEventBus localEventBus, ILogger<VibAlarmEventPublisher> logger)
    {
        _localEventBus = localEventBus;
        _logger = logger;
    }
    
    public Task PublishAsync(Guid pointId, DateTime timestamp)
    {
        _logger.LogInformation("发布振动报警事件 VibAlarmEvent, PointId: {pointId}, Timestamp: {timestamp}", pointId, timestamp);
        
        return _localEventBus.PublishAsync(new VibAlarmEvent
        {
            PointId = pointId,
            Timestamp = timestamp
        });
    }
}

/// <summary>
/// 振动报警事件
/// </summary>
public class VibAlarmEvent
{
    /// <summary>
    /// 测点 Id
    /// </summary>
    public Guid PointId { get; set; }

    /// <summary>
    /// 报警 Ts（Utc 时间）
    /// </summary>
    public DateTime Timestamp { get; set; }
}